<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="userName">
                <el-input v-model="ruleForm.userName"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password" :show-password="true"></el-input>
            </el-form-item>
            <el-form-item label="头像上传" prop="password">
                <el-upload class="avatar-uploader" action="http://localhost:5000/uploadFile" name="formFile"
                    :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="选择角色" prop="password">
                <el-select v-model="ChkRole" multiple placeholder="请选择" style="width: 100%;">
                    <el-option v-for="item in RoleData" :key="item.roleId" :label="item.roleName" :value="item.roleId">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import axios from '../../util/request';
    export default {
        name: 'CodeUIAdminCreate',

        data() {
            return {
                ruleForm: {
                    userName: '',
                    password: '',
                    avatar: '',
                    intRoles: [],
                    strRoles: ''
                },
                rules: {
                    userName: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ]
                },
                RoleData: [],
                imageUrl: '',
                ChkRole: []
            };
        },

        mounted() {
            axios.get('/Role/QueryAll').then(m => {
                this.RoleData = m.data;
            });
        },

        methods: {
            handleAvatarSuccess(res, file) {
                this.ruleForm.avatar = res.filePath;
                this.imageUrl = URL.createObjectURL(file.raw);
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            submitForm(formName) {                
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.ruleForm.intRoles = this.ChkRole;
                        this.ruleForm.strRoles = this.ChkRole.toString();
                        axios.post('/admin/create', this.ruleForm).then(m => {
                            //1、弹出消息
                            if (m.data.code > 200) {
                                this.$message({
                                    message: m.data.message,
                                    type: 'warning'
                                });
                            }
                            else {
                                this.$message({
                                    message: '添加成功',
                                    type: 'success'
                                });
                                //2、关闭弹窗
                                this.$emit('OnSave')
                            }                            
                            //3、刷新列表
                        });
                    } else {
                        return false;
                    }
                });
            },
        },
    };
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 88px;
        height: 88px;
        line-height: 88px;
        text-align: center;
    }

    .avatar {
        width: 88px;
        height: 88px;
        display: block;
    }
</style>